<?php

/*
 * This file is part of PHP ENTITY LINK.
 *
 *  PHP ENTITY LINK is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  PHP ENTITY LINK is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with PHP ENTITY LINK.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Copyright (C) 2010 Peter Jacques, Michael Kyndt
 *
*/

namespace php_entity_link{    

    /**
     * Represents one record form a table
     *
     * @author Peter Jacques
     * @@package php entity link
     * @version 1.0.0
     */
    class Entity {

        /**
         * The name of the table of the record
         * @var <string>
         */
        private $table;
        /**
         * Holds the record data
         * @var <array> key value pairs
         */
        private $params;
        /**
         * Indicates wheter extra parameters can be added to the record data.
         * TRUE => not allowd (default) => FALSE Allowed. When allowed and done
         * updating, inserting the record will result in errors
         * @var <boolean> DAFULT: true
         */
        private $strict_set;

        /**
         * Constructor for the creating of an instance
         * @param <string> $table table name of the record
         * @param <array> $params (key value pairs) record data
         * @param <boolean> $strict_set Indicates wheter extra parameters can be
         *  added to the record data
         */
        public function __construct($table,$params,$strict_set=true) {
            $this->table = $table;
            $this->params = $params;
            $this->strict_set = $strict_set;
        }//__construct

        /**
         * Returns the table name of the record
         * @return <string>
         */
        public function getTable() {
            return $this->table;
        }//getTable

        /**
         * Returns the record data
         * @return <type>
         */
        public function getParams() {
            return $this->params;
        }//getParams

        /**
         * Compares 2 objects. Compares the record data and table name
         * @param <Entity> $object
         * @return <boolean> true if equal, false if not
         */
        public function equals(Entity $object) {
            $params = $object->getParams();
            foreach($params as $key => $value) {
                if(array_key_exists($key, $this->params) && $this->params[$key] == $value) {
                    continue;
            }
            else {
                    return false;
                }
            }//next key=>value
            if($object->getTable() != $this->table) {
                return false;
            }

            return true;
        }//compare

        /**
         * Magic get method. Returns the value from a record field.
         * Triggers fatal error when accessing an record field that doesn't exists
         * if strict_set is true.
         * @example $value = $object->title
         * @param <type> $name fieldname
         * @return <type> field value
         */
        public function  __get($name) {
            if(array_key_exists($name, $this->params)) {
                return $this->params[$name];
            }else {
                $debug_backtrace = debug_backtrace();
                $error_msg = "Call to undefined property '".$name."' via __get()";
                $error_msg .=" in ".$debug_backtrace[0]['file']." on line ".$debug_backtrace[0]['line'];
                trigger_error($error_msg, E_USER_ERROR);
            }
        }//__get

        /**
         * Magic set method. Sets the value form a record field.
         * Triggers fatal error when accessing an record field that doesn't exists
         * if strict_set is true.
         * @example $value = $object->title
         * @param <type> $name fieldname
         */
        public function  __set($name,  $value) {
            if($this->strict_set && !array_key_exists($name, $this->params)) {
                $debug_backtrace = debug_backtrace();
                $error_msg = "Call to undefined property '".$name."' via __set()";
                $error_msg .=" in ".$debug_backtrace[0]['file']." on line ".$debug_backtrace[0]['line'];
                trigger_error($error_msg, E_USER_ERROR);
            }else {
                $this->params[$name] = $value;
            }
        }//__set

        /**
         * Magic method. Checks if a record field is set.
         * @example isset($object->title)
         * @param <type> $name fieldname
         * @return <boolean> true if set otherwise false
         */
        public function __isset($name) {
            return isset($this->params[$name]);
        }//__isset

        /*
         * Magic method. Unsets a record field.
         * Triggers fatal error when accessing an record field that doesn't exists
         * if strict_set is true.
         * @example unset($object->title)
         * @param <type> $name fieldname
        */
        public function __unset($name) {
            if($this->strict_set) {
                $this->$name = null;
            }else {
                unset($this->params[$name]);
            }
        }//__unset

        /**
         * Magic to string method. Returns a string representation form the
         * objects record data.
         * @return <string>
         */
        public function  __toString() {
            $str = "";
            foreach($this->params as $key=>$value) {
                $str .= ", ".$key." => ".$value;
            }//next key=>value
            $str = substr($str, 2);
            return $str;
        }//__toString

    }//Entity

}//php_entity_link

?>